#include "ubt_com/ubt_com_util.h"
#include "ubt_com/ubt_proto_adjust.h"

namespace ubt_proto_adjust
{

int ubtAdjustEncodeComRsp(std::string&  buf, ubt_adjust_msg_com_rsp *pComRsp, std::string& failDesc)
{
    char bufTmp[64];
    sprintf(bufTmp,"result=%d&desc=", pComRsp->result);
    buf += std::string(bufTmp)+pComRsp->desc;	
    return 0;
}

int ubtAdjustEncodeStartReq(std::string&  buf, ubt_adjust_msg_start_req *pStartReq, std::string& failDesc)
{
    buf += "taskId=" + pStartReq->taskId;
    char bufTmp[256];
    sprintf(bufTmp, "&lifeTime=%d&poseType=%d&poseDst.x=%lf&poseDst.y=%lf&poseDst.theta=%lf&poseLmt.x=%lf&poseLmt.y=%lf&poseLmt.theta=%lf&isPause=%d",
            pStartReq->lifeTime, pStartReq->poseType, pStartReq->poseDst.x, pStartReq->poseDst.y, pStartReq->poseDst.theta,
            pStartReq->poseLmt.x, pStartReq->poseLmt.y, pStartReq->poseLmt.theta, pStartReq->isPause);
    buf += std::string(bufTmp);
    return 0;
}


int ubtAdjustEncodeStopReq(std::string&  buf, ubt_adjust_msg_stop_req *pStopReq, std::string& failDesc)
{
    char bufTmp[256];
    sprintf(bufTmp, "iCleanInfo=%d&duration=%d&runStatus=%d",
            pStopReq->iCleanInfo, pStopReq->duration, pStopReq->runStatus);
    buf += std::string(bufTmp);
    return 0;
}

int ubtAdjustEncodeStatus(std::string&  buf, ubt_adjust_status *pStatus, std::string& faiDesc)
{
    buf += "taskId=" + pStatus->taskId;
    char bufTmp[1024];
    sprintf(bufTmp, "&ttl=%d&flag=%d&runStatus=%d&isPoseOffVirtual=%d&poseOff.x=%lf&poseOff.y=%lf&poseOff.theta=%lf&runDesc=%s",
            pStatus->ttl, pStatus->flag, pStatus->runStatus, pStatus->isPoseOffVirtual,
            pStatus->poseOff.x, pStatus->poseOff.y, pStatus->poseOff.theta, pStatus->runDesc.c_str()
           );
    buf += std::string(bufTmp);
    return 0;
}

int ubtAdjustDecodeComRsp(std::string& buf, ubt_adjust_msg_com_rsp *pComRsp, std::string& failDesc)
{
    std::map<std::string, std::string> paramMap = ubt_com::ParseUrlQuery(buf);
    pComRsp->result = 0;
    pComRsp->desc = "";
    for (std::map<std::string, std::string>::iterator iter = paramMap.begin(); iter != paramMap.end(); ++iter)
    {
        if (iter->first == "result")
        {
            pComRsp->result = atoi(iter->second.c_str());
        }
        else if (iter->first == "desc")
        {
            pComRsp->desc = iter->second;
        }
        else
        {
            ;
        }
    }
    return 0;
}


int ubtAdjustDecodeStartReq(std::string& buf, ubt_adjust_msg_start_req *pStartReq, std::string& failDesc)
{
    std::map<std::string, std::string> paramMap = ubt_com::ParseUrlQuery(buf);
    pStartReq->taskId = "";
    pStartReq->lifeTime = 180;
    pStartReq->poseType = ubt_adjust_pose_odom;
    pStartReq->poseDst.x = pStartReq->poseDst.y = pStartReq->poseDst.theta = 0;
    pStartReq->poseLmt.x = pStartReq->poseLmt.y = pStartReq->poseLmt.theta = 0;
    pStartReq->isPause = 0;
    for (std::map<std::string, std::string>::iterator iter = paramMap.begin(); iter != paramMap.end(); ++iter)
    {
        if (iter->first == "taskId")
        {
            pStartReq->taskId = iter->second;
        }
        else if (iter->first == "lifeTime")
        {
            pStartReq->lifeTime = atoi(iter->second.c_str());
        }
        else if (iter->first == "poseType")
        {
            pStartReq->poseType = (ubt_adjust_pose_type)atoi(iter->second.c_str());
        }
        else if (iter->first == "poseDst.x")
        {
            pStartReq->poseDst.x = atof(iter->second.c_str());
        }
        else if (iter->first == "poseDst.y")
        {
            pStartReq->poseDst.y = atof(iter->second.c_str());
        }
        else if (iter->first == "poseDst.theta")
        {
            pStartReq->poseDst.theta = atof(iter->second.c_str());
        }
        else if (iter->first == "poseLmt.x")
        {
            pStartReq->poseLmt.x = atof(iter->second.c_str());
        }
        else if (iter->first == "poseLmt.y")
        {
            pStartReq->poseLmt.y = atof(iter->second.c_str());
        }
        else if (iter->first == "poseLmt.theta")
        {
            pStartReq->poseLmt.theta = atof(iter->second.c_str());
        }
        else if (iter->first == "isPause")
        {
            pStartReq->isPause = atoi(iter->second.c_str());
        }
        else
        {

        }
    }
    return 0;
}

int ubtAdjustDecodeStopReq(std::string&  buf, ubt_adjust_msg_stop_req *pStopReq, std::string& failDesc)
{
    std::map<std::string, std::string> paramMap = ubt_com::ParseUrlQuery(buf);
    pStopReq->iCleanInfo = 0;
    pStopReq->duration = 0;
    pStopReq->runStatus = ubt_adjust_run_status_cancel;
    for (std::map<std::string, std::string>::iterator iter = paramMap.begin(); iter != paramMap.end(); ++iter)
    {
        if (iter->first == "iCleanInfo")
        {
            pStopReq->iCleanInfo = atoi(iter->second.c_str());
        }
        else if (iter->first == "duration")
        {
            pStopReq->duration = atoi(iter->second.c_str());
        }
        else if (iter->first == "runStatus")
        {
            pStopReq->runStatus = atoi(iter->second.c_str());
        }
        else
        {
            ;
        }
    }
    return 0;
}

int ubtAdjustDecodeStatus(std::string& buf, ubt_adjust_status *pStatus, std::string& failDesc)
{
    std::map<std::string, std::string> paramMap = ubt_com::ParseUrlQuery(buf);
    pStatus->taskId = "";
    pStatus->ttl = 0;
    pStatus->flag = 0;
    pStatus->runStatus = 0;
    pStatus->runDesc = "";
    pStatus->poseOff.x = pStatus->poseOff.y = pStatus->poseOff.theta = 0;
    for (std::map<std::string, std::string>::iterator iter = paramMap.begin(); iter != paramMap.end(); ++iter)
    {
        if (iter->first == "taskId")
        {
            pStatus->taskId = iter->second;
        }
        else if (iter->first == "ttl")
        {
            pStatus->ttl = atoi(iter->second.c_str());
        }
        else if (iter->first == "flag")
        {
            pStatus->flag = atoi(iter->second.c_str());
        }
        else if (iter->first == "runStatus")
        {
            pStatus->runStatus = atoi(iter->second.c_str());
        }
        else if (iter->first == "runDesc")
        {
            pStatus->runDesc = iter->second;
        }
        else if (iter->first == "isPoseOffVirtual")
        {
            pStatus->isPoseOffVirtual = atoi(iter->second.c_str());
        }
        else if (iter->first == "poseOff.x")
        {
            pStatus->poseOff.x = atof(iter->second.c_str());
        }
        else if (iter->first == "poseOff.y")
        {
            pStatus->poseOff.y = atof(iter->second.c_str());
        }
        else if (iter->first == "poseOff.theta")
        {
            pStatus->poseOff.theta = atof(iter->second.c_str());
        }
        else
        {
            ;
        }
    }
    return 0;
}


}
